package com.tailyou.androidlearning2426.kotlin

// 类
// 父类：人
open class Person(var name: String, var age: Int, var height: Float, var gender: String) {
    // 方法
    fun playGame(name: String) {
        println("Like play $name")
    }

    fun isAdult(): Boolean {
        return age >= 18
    }

    override fun toString(): String {
        return "Student(name='$name', age=$age, height=$height, gender='$gender')"
    }
}

/**
 * 子类：学生类
 */
class Student(var stuId: String, name: String, age: Int, height: Float, gender: String) :
    Person(name, age, height, gender) {

    /**
     * 根据学号获取入学年份
     */
    fun getGrade(): String {
        // 获取stuId 学号前4位，并转换成整型
        return stuId.substring(0, 4)
    }
}

fun main() {
    // 创建对象不需要关键字new
    val yt = Person("YuanTao", 18, 180f, "Male")
    println(yt)
    println("${yt.name}  ${yt.isAdult()}")

    val zym = Person("ZhengYiMan", 19, 165f, "Female")
    println(zym)
    zym.playGame("CS")

    // 创建子类的对象
    val yzs = Student("202403010003", "YanZeSheng", 18, 175f, "Male")
    println("${yzs.name}  ${yzs.isAdult()}")
    yzs.playGame("WZRY")
    println(yzs.getGrade())
}